package com.tepth.vehiclemaintenance.mvp.work;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.adapter.FragmentAdapter;
import com.tepth.vehiclemaintenance.bean.VehicleMaintain;
import com.tepth.vehiclemaintenance.event.StartBrotherEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by zhangyu on 2017/5/10.
 */

public class WorkFragment extends SupportFragment {
    private static final String PARAMETER = "vehicleMaintain";
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tab)
    TabLayout mTab;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    private VehicleMaintain mVehicleMaintain;
    private Unbinder unbinder;

    public static WorkFragment newInstance(VehicleMaintain vehicleMaintain) {
        WorkFragment fragment = new WorkFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(PARAMETER, vehicleMaintain);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mVehicleMaintain = (VehicleMaintain) bundle.getSerializable(PARAMETER);
        }
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_work, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        EventBus.getDefault().register(this);
        mToolbar.setTitle(mVehicleMaintain.getVehicleNumber() + "保养作业");
        mTab.addTab(mTab.newTab().setText("保养作业"));
        mTab.addTab(mTab.newTab().setText("保养历史"));
        initToolbarNav(mToolbar);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mViewPager.setAdapter(new FragmentAdapter(getChildFragmentManager(), mVehicleMaintain));
        mTab.setupWithViewPager(mViewPager);
    }

    protected void initToolbarNav(Toolbar toolbar) {
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
    }
    @Subscribe
    public void startBrother(StartBrotherEvent event) {
        start(event.targetFragment);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
        super.onDestroyView();
    }

}
